package com.xinchuang.initializrdemo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component //让spring能够扫描到
@Aspect //定义这是一个切面类
public class LogAspect {

    //定义切点
//    @Pointcut("execution(* com.xinchuang.initializrdemo.service.UserService.getUserList())")
    @Pointcut("execution(* com.xinchuang.initializrdemo.service.*.*(..))")
    public void pointCut() {

    }
    /**
     * before 目标方法执行前执行
     * after 目标方法执行后执行
     * after returning 目标方法返回时执行 ，后置返回通知
     * after throwing 目标方法抛出异常时执行 异常通知
     * around 在目标函数执行中执行，可控制目标函数是否执行，环绕通知
     */

    @Before("pointCut()")
    public void before(){
        System.out.println("before.......");
    }


    @After("pointCut()")
    public void after(){
        System.out.println("After.........");
    }

    @AfterReturning("pointCut()")
    public void afterReturning(){
        System.out.println("AfterReturning........");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        System.out.println("AfterThrowing..........");
    }

    /**
     * 可控制目标函数是否执行
     */
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("方法执行前....");
        Object proceed = joinPoint.proceed();
        System.out.println("方法执行后....");
        return proceed;
    }
}
